package ace.module.upms.core.impl.service.impl;

import ace.module.upms.api.constant.UpmsConstant;
import ace.module.upms.core.impl.dao.aggregate.UpmsMenuAggregate;
import ace.module.upms.core.impl.dao.entity.UpmsMenu;
import ace.module.upms.core.impl.service.UmpsAuthorityService;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/8/18 17:42
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class UmpsAuthorityServiceImpl implements UmpsAuthorityService {
  public static final String CACHE_AREA = UpmsConstant.MODULE_NAME + "_authority";
  public static final String CACHE_CAPTCHA_ITEM = UpmsConstant.MODULE_NAME + ":authority";
  private final UpmsMenuAggregate upmsMenuAggregate;

  @Cached(
      area = CACHE_AREA,
      name = CACHE_CAPTCHA_ITEM,
      cacheType = CacheType.BOTH,
      key = "args[0]+':'+args[1]")
  @Override
  public Set<String> getByUpmsAppIdAndAccountId(Long upmsAppId, Long accountId) {
    List<String> authorities =
        upmsMenuAggregate
            .getByUpmsAppIdAndAccountIdAndDeleteFlagIsNot(upmsAppId, accountId)
            .stream()
            .map(UpmsMenu::getPermission)
            .collect(Collectors.toList());
    return new HashSet<>(authorities);
  }
}
